﻿using System;

// XNA lib
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

//Oak Engine Lib
using OakEngine.GameState;
using OakEngine.Input;
using Microsoft.Xna.Framework.Input;
using MainJumpFleet.SteerBehaviors;
using OakEngine.AI.Kinematic;
using OakEngine.AI.Steering;

namespace MainJumpFleet.GameStates
{
   public class MovementAIScreenState : OAK_IGameState
    {
                #region OAK_IGameState Members
        SpriteBatch batch;
        SpriteFont mainMenuFont;
        Texture2D dot;
        Vector2 origin;
        Vector2 move;

        Oak_Kinematic[] agents;
        Seek seek;
        Flee flee;
        newWander newWander;
       private Wander wander;
        SteeringBehavior[] curentBehavior;
        Oak_Steering steerseek;
        Oak_Steering steerflee;
       

        SteeringBehavior sb;
        SteeringBehavior fb;
        SteeringBehavior wb;

       // new test
        KinematicSeek ks;
        KinWander kw;
        OakEngine.AI.Waypoint.Oak_Waypoint wp;
        KinematicMovement km;

        public MovementAIScreenState()
        {
            Enter();
        }

        float speed = 150.5f;
        public void Enter()
        {
            mainMenuFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>("Fonts\\mainMenuFont");
            batch = CGame.CGame.GetInstance().SpriteBatch;
            dot = CGame.CGame.GetInstance().Content.Load<Texture2D>("Debug\\dot");
            origin = new Vector2(dot.Width * 0.5f, dot.Height * 0.5f);


            // Load agents
            agents = new Oak_Kinematic[2];
            agents[0] = new Oak_Kinematic();
            agents[1] = new Oak_Kinematic();
           // sb = new Seek();
           // fb = new Flee();

            agents[0].Position = new Vector2(330,330);
            agents[0].Direction = new Vector2();
            agents[0].AngularVelocity = 0;
            agents[0].Rotation = 2.1f;

            agents[1].Position = new Vector2(200, 300);
            agents[1].Direction = new Vector2();
            agents[1].Rotation = 0.75f;
            agents[1].AngularVelocity = 0;

            // set up seek and flee
            seek = new Seek();
            flee = new Flee();
            wander = new Wander();
            newWander = new newWander();
            seek.m_Agent = agents[0];
            seek.m_Target = agents[1].Position;
            seek.m_fMaxAcceleration = speed;

            flee.m_Agent = agents[1];
            flee.m_Target = agents[0].Position;
            flee.m_fMaxAcceleration = speed;

            wander.m_Agent = agents[0];
            wander.m_fAcceleration = speed;
            wander.m_fAimRandomly = 20.0f;
            wander.m_fTurnSpeed = 2.0f;

            newWander.m_Agent = agents[0];;
            newWander.m_fMaxAcceleration = speed;
            newWander.volatility = 100.0f;
            newWander.turnSpeed = 2.0f;

            sb = seek;
            fb = flee;
            wb = newWander;
            steerseek = new Oak_Steering();
            steerflee = new Oak_Steering();
            /////////////////////////////////////////////////////////////////////////////////////////////////
            // new seek
            wp = new OakEngine.AI.Waypoint.Oak_Waypoint();
            wp.m_Position = new Vector2(450,200);
            wp.m_fRotation = 2.1f;


            ks = new KinematicSeek();
            kw = new KinWander();
            kw.location = wp;
            kw.speed = speed;
            kw.rotation = 0.0f;
           // ks.location = wp;
            //ks.target = new Vector2(500, 200);
            //ks.speed = 200.0f;

            //km = ks;
            km = kw;

            move = new Vector2(200,500);
        }

        public bool Input(OAK_InputManager input)
        {

            if (input.Pressed(Keys.Escape) || input.Pressed(Buttons.Back, 0))
                CGame.CGame.GetInstance().ChangeState(new MainMenuState());

            //seek.m_Target = input.MousePosition;
            //sb = seek;
            //flee.m_Target = input.MousePosition;
            //fb = flee;

            //ks.target = input.MousePosition;
            //km = ks;


            if (input.HeldDown(Keys.A))
                move.X += -1.0f;

            if (input.HeldDown(Keys.D))
                move.X += 1.0f;
            

            return false;
        }
        float bucket = 2;
        public void Update(GameTime dt)
        {


          //  steerseek.ZeroOut();
            steerflee.ZeroOut();
            //sb.UpdateSteering(steerseek);
           // fb.UpdateSteering(steerflee);
           // if (bucket > 0.5)
            {
                wb.UpdateSteering(steerseek);
                bucket = 0;
            }
            bucket +=(float) dt.ElapsedGameTime.TotalSeconds;

            agents[0].Update(steerseek, 0.95f, (float)dt.ElapsedGameTime.TotalSeconds);
            agents[0].SetRotationFromVelocity();

             //check for max speed
            agents[0].TrimSpeed(speed);


            //agents[1].Update(steerflee, 0.95f, (float)dt.ElapsedGameTime.TotalSeconds);
            //agents[1].SetRotationFromVelocity();

            //  check for max speed
            //agents[1].TrimSpeed(speed);


            //seek.m_Target = agents[1].Position;
            //sb = seek;

            //flee.m_Target = agents[0].Position;
            //fb = flee;

            km.getSteering(steerseek);
            wp.Update(steerseek, (float)dt.ElapsedGameTime.TotalSeconds);
            wp.SetRotationFromVelocity(steerseek.ForwardVector);

            // WOrld Bound Check
            int width = CGame.CGame.GetInstance().Graphics.PreferredBackBufferWidth;
            int height = CGame.CGame.GetInstance().Graphics.PreferredBackBufferHeight;


            for (int i = 0; i < 2; i++)
            {
                Vector2 temp = agents[i].Position;
                WorldCheck(ref temp, new Rectangle(0, 0, width, height));
                agents[i].Position = temp;
       
            }



        }

        public void Render()
        {
            batch.DrawString(mainMenuFont, "Target: " + newWander.m_Target.ToString(), new Vector2(30, 50), Color.White);
            batch.DrawString(mainMenuFont, "Direction: " + agents[0].Direction.ToString(), new Vector2(30, 150), Color.White);
            batch.DrawString(mainMenuFont, "Position: " + agents[0].Position.ToString(), new Vector2(30, 200), Color.White);
            batch.DrawString(mainMenuFont,"Rotation: "+ agents[0].Rotation.ToString(), new Vector2(30, 250), Color.White);
          //  batch.DrawString(mainMenuFont,"Steer: " + steer.ForwardVector.ToString(), new Vector2(30, 350), Color.White);


            //batch.Draw(dot, wp.m_Position, null, Color.Red, wp.m_fRotation, origin, 1, SpriteEffects.None, 0);
            batch.Draw(dot, agents[0].Position, null, Color.Blue, agents[0].Rotation, origin, 1, SpriteEffects.None, 0);
            
        }

        public void Exit()
        {
        }

        private void WorldCheck( Vector2 inVec, Rectangle bounds)
        {
            if (-1.0f > inVec.X)
                inVec.X = bounds.Width;
            if (bounds.Width < inVec.X)
                inVec.X = -5.0f;

            if (-1.0f > inVec.Y)
                inVec.Y = bounds.Height;
            if (bounds.Height < inVec.Y)
                inVec.Y = -2.0f;


        }

       private void WorldCheck(ref Vector2 inVec,Rectangle bounds)
       {
           if (-10.0f > inVec.X)
               inVec.X = bounds.Width;
            if (bounds.Width < inVec.X)
               inVec.X = -10.0f;

            if (-10.0f > inVec.Y)
               inVec.Y = bounds.Height;
            if (bounds.Height < inVec.Y)
               inVec.Y = -10.0f;
 
             
       }

        #endregion
    }
}
